package cn.com.cfae.iras.doc.analyze.components.rationality;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.handUtil.HandingData;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.DocumentModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 内容合理性
 * M-3-1
 * 票据利率部分确定方式是否填写内容，是否包含“赎回权”、“回售权”和“转售权”。
 * 发行阶段
 */
@Component("R1002")
@Scope("prototype")
public class R1002 extends BaseComponent {
    private static Logger log = LoggerFactory.getLogger(R1002.class);
    @Autowired
    private HandingData handingData;
    @Override
    public List<ComponentResult> handlerM() {
        log.info("获取R1002组件M");
        List<ComponentResult> componentResults = checkRulesR1002(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        log.info("获取R1002组件BM");
        List<ComponentResult> componentResults = checkRulesR1002(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        log.info("获取R1002组件DM");
        List<ComponentResult> componentResults = checkRulesR1002(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        log.info("获取R1002组件BDM");
        List<ComponentResult> componentResults = checkRulesR1002(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }


    private List<ComponentResult> checkRulesR1002(String model,String catalogId,String chapterId,String ruleName){
        DocumentModel extractData = super.getExtractData();
        List<ComponentResult> componentResults = new ArrayList<>();
        ComponentResult pjllComponentResult = null;
        ExtractItem extractItem = null;
        int pjllPageNum = 0;
        List<ComponentResult.TextPosition> pjllContentAndPositionList = null;

        String[] contents = new String[]{"赎回权","回售权","转售权"};
        int startPage = 0;
        boolean isAllErr = false;
        try {

            if (extractData!=null){
                ChapterModel chapterModel = extractData.getChapterModel(model);
                startPage = chapterModel.getStartPage();
                //票据利率
                extractItem = getExtractItem(extractData,model,"FXTK","PJLL");
                if (extractItem!= null){
                    KVText value = extractItem.getValue();
                    KVText key = extractItem.getKey();
                    String text = value!=null?value.getText():null;
                    pjllPageNum = value!=null?value.getPageNumber():startPage;
                    pjllPageNum = pjllPageNum!=0?pjllPageNum:startPage;
                    pjllContentAndPositionList = key!=null?getContentAndPositionList(key):null;

                    if (StringUtils.isNotEmpty(text)) {
                        for (String content : contents) {
                            if (text.contains(content)) {
                                isAllErr = true;
                                pjllComponentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"票据利率关键词检查-"+content,"已包含,建议人工核对",true,getRegulationId(), pjllPageNum,pjllContentAndPositionList,"02");
                                componentResults.add(pjllComponentResult);
                            }else {
                                pjllComponentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"票据利率关键词检查-"+content,"未包含,建议人工核对",false,getRegulationId(), pjllPageNum,pjllContentAndPositionList,"03");
                                componentResults.add(pjllComponentResult);
                            }
                        }
                    }else {
                        pjllComponentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"票据利率关键词检查","内容可能缺失,建议人工核对",true,getRegulationId(), pjllPageNum,pjllContentAndPositionList,"03");
                        componentResults.add(pjllComponentResult);
                    }
                }else {
                    for (String content :contents){
                        pjllComponentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"票据利率关键词检查-"+content,"未识别,建议人工核对",true,getRegulationId(), startPage,pjllContentAndPositionList,"03");
                        componentResults.add(pjllComponentResult);
                    }
                }
            }

           /* if (isAllErr){
                for (ComponentResult result : componentResults){
                    result.setExist("02");
                }
            }*/
        }catch (Exception e){
            componentResults.clear();
            for (String content :contents){
                pjllComponentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"票据利率关键词检查-"+content,"未识别,建议人工核对",true,getRegulationId(), startPage,pjllContentAndPositionList,"03");
                componentResults.add(pjllComponentResult);
            }
            log.error(e.getMessage(),e);
        }

        return  componentResults;
    }



    private ExtractItem getExtractItem(DocumentModel extractData, String chapter, String model, String key){
        ExtractItem extractItem  = null;
        if (extractData!=null){
            ChapterModel chapterModel = extractData.getChapterModel(chapter);
            if (chapterModel != null){
                ExtractBizModel extractMode = chapterModel.getModel(model);
                if (extractMode!=null){
                    extractItem = extractMode.getExtractItem(key);
                }
            }
        }
        return extractItem;
    }

}
